package com.example.sdk;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

import java.net.URISyntaxException;

public class AliPayManager {
    private static final String PACKAGE_NAME_ALIPAY = "com.eg.android.AlipayGphone";

    // 支付宝首页
    private static final String url = "alipayqr://platformapi/startapp?";
    // 支付宝扫一扫页面
    //private static final String url = "alipayqr://platformapi/startapp?saId=10000007";

    // 生成环境
    //private static final  String url = "alipays://platformapi/startapp?appId=20000067&url=https%3A//polycode.kjtpay.com/purse%3Ftoken%3Dcbffc0aa9c4d4f3994743008950bb406";

    // 准生成环境
    //private static final  String url = "alipays://platformapi/startapp?appId=20000067&url=https%3A//zpolycode.kjtpay.com/purse%3Ftoken%3Dcbffc0aa9c4d4f3994743008950bb406";

    public static void open(Context context) {
        // 检查支付宝是否安装；
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(PACKAGE_NAME_ALIPAY, 0);
            if(null == info) {
                Toast.makeText(context, "没有安装支付宝", Toast.LENGTH_LONG);
                return;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return;
        }

        // 如果手机没有支付宝，下面的代码会导致APP崩溃，try... catch也没用
        // 拉起支付宝
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }
}
